home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / program / argv_akp.txt < prev    next >
Text File  |  1994-08-27  |  20KB  |  466 lines

  1. ------------------------------
  2.  
  3. Date: 9 May 91 21:18:36 GMT
  4. From: imagen!atari!apratt@sun.com (Allan Pratt)
  5. Subject: ARGV spec repost as promised
  6. To: Info-Atari16@naucse.cse.nau.edu
  7.  
  8. In a discussion about Pexec and command lines, the question of the
  9. ARGV standard came up.  Here is our documentation on this convention.
  10. It dates from August 22, 1990.
  11.  
  12. GEMDOS EXTENDED ARGUMENT (ARGV) SPECIFICATION
  13.  
  14. Introduction
  15.  
  16. The Pexec() function of GEMDOS allows a program to pass to a child
  17. process a command line up to 125 characters long, with arguments
  18. separated by spaces.  No provision is made in GEMDOS for the child to
  19. know its own name.  This makes it difficult for C programs to correctly
  20. fill in argv[0], the standard place where a C program finds the command
  21. which invoked it.  Because the command line arguments are separated by
  22. spaces, it is difficult to pass an argument with an embedded space.
  23. This document will specify a method of passing arguments which allows
  24. arbitrary argument length, embedded spaces, and support for argv[0].
  25.  
  26. Standard Argument Passing
  27.  
  28. The Pexec Cookbook specifies how to use Pexec() to launch a child
  29. process, passing a command tail (argument string) and an environment.
  30. Before getting into the extended argument scheme, let's review how
  31. arguments are normally passed to a child.
  32.  
  33. A parent process builds a command line into an argument string - a null
  34. terminated string whose first byte contains the length of the rest of
  35. the string - and its address is passed as one of the arguments to
  36. Pexec().  GEMDOS copies this argument string to the basepage which it
  37. creates for the child.  Thus the parent is responsible for gathering
  38. all the child's arguments into one string.  This is normally handled by
  39. a library exec() function.  The child is responsible for parsing the
  40. string of space-separated arguments back into an array of strings.
  41. This parsing is normally handled by the child's startup code.
  42.  
  43. Evolution
  44.  
  45. Several methods of bypassing the limits imposed by Pexec() have been
  46. used by GEMDOS programs.  Some allow a user to specify a file on the
  47. command line which contains the rest of the arguments.  Others get a
  48. pointer to the arguments, or the arguments themselves, from the
  49. environment string.  Most MS-DOS programs use a command file for the
  50. extra arguments.  This can be inconvenient for a user, cluttering the
  51. file system with command files, and making the operation of batch files
  52. and makefiles more confusing.
  53.  
  54. Several "standards" have arisen on the ST which use the environment to
  55. pass arguments.  While more convenient than command files, these
  56. standards have other problems.  Some rely on sharing memory between
  57. parent and child processes.  Some take advantage of undocumented
  58. features of the operating system to get argv[0].  Others give the
  59. child process no way to validate that the arguments it finds are
  60. intended for it.
  61.  
  62. Rationale
  63.  
  64. In order to pass more than the standard 125 characters worth of
  65. arguments to a child, or to let the child find its name, the parent
  66. must place the extra information in a place where the child can access
  67. it safely and legally.  The most convenient place is in the child's
  68. environment string.  An environment string is a series of
  69. null-terminated strings of the format "VARIABLE=value" (e.g.
  70. PATH=c:\bin,c:\etc, or ShellP=YES).   The last null-terminated string
  71. in the environment is followed by a zero byte, thus two consecutive
  72. nulls indicates the end of the environment.   The environment is
  73. allocated for the child by GEMDOS, it is owned by the child, and its
  74. contents can be specified by the parent.
  75.  
  76. The child must have some way of knowing that the arguments which
  77. it finds in its environment are intended for it.  The child may have
  78. been invoked by a parent which does not conform to this specification.
  79. Such a parent would leave _its_ arguments in the environment, and could
  80. pass that environment on to the child.  The child would mistakenly
  81. interpret its parent's arguments as its own.
  82.  
  83. Placing arguments in the environment passed to the child gets around
  84. all of the command line limits of the standard Pexec() command tail.
  85. Because there is no limit on the length of the environment, arbitrary
  86. length arguments are supported.  Arguments placed in the environment
  87. are null terminated, so they may contain spaces.  A parent can also
  88. place the name of the command with which it invokes the child in the
  89. child's environment, providing support for argv[0].  Validation of the
  90. extended arguments can be placed in the standard Pexec() command line,
  91. by assigning a special meaning to an invalid length byte.
  92.  
  93. The GEMDOS Extended Argument Specification
  94.  
  95. This specification uses the convention that the presence of an
  96. environment variable named ARGV (all upper case) indicates that extended
  97. arguments are being passed to the child in its environment.  This means
  98. that ARGV is a "boolean" environment variable.  For the purpose of this
  99. specification, its value is not significant, but its presence indicates
  100. that the strings following it are the arguments for the child.
  101. Implementations of this specification are free to give the ARGV
  102. environment variable any value.  The ARGV environment variable must be
  103. the last one in the environment passed to the child, so that the child
  104. can truncate its environment at that point, and treat everything before
  105. the ARGV as environment, and everything after it as arguments.
  106.  
  107. The first argument to the child (argv[0]) is the first string in the
  108. environment after the ARGV variable.  This argument is the "pathname"
  109. parameter passed by the parent to Pexec().  The remaining arguments are
  110. those that the child would normally find in the command tail in its
  111. basepage.  Even if all of the arguments would normally fit in a child's
  112. command tail, the parent should set up the arguments in the environment
  113. to take advantage of the benefits of this extended argument scheme.
  114.  
  115. As many arguments as will fit in the command tail will be passed there
  116. as well as in the environment, to support non-conforming programs.  As
  117. a flag that arguments are also in the environment, the length byte of
  118. the command tail will be 127 (hex 7f).  Non-conforming programs should
  119. not have a problem with this length byte, because it is longer than the
  120. maximum 125 bytes allowed by Pexec().
  121.  
  122. As an aside, the Pexec Cookbook erroneously implies that a command line
  123. can be 126 or 127 characters long.  In fact, GEMDOS only copies to the
  124. child's basepage up to 125 bytes, or until it encounters a null, from
  125. the argument string passed to Pexec().  It ignores the length byte,
  126. placing a null at the same place it found one or at the 126th byte if
  127. no null is found.  This has several implications: the length byte is
  128. not validated by GEMDOS (necessitating validation in the child's
  129. startup code, but also making this extended argument spec possible),
  130. and the null terminator _can_ be located after the end of the real
  131. command tail (the Desktop places a CR character after the command tail
  132. and before the null).  The ARGSTART.S startup code listing below
  133. demonstrates how to correctly validate and parse a GEMDOS command tail.
  134.  
  135. A child which finds an ARGV environment variable can use the command
  136. tail length byte value of 127 to validate that the arguments following
  137. the variable are valid, and not just left over from a non-conforming
  138. parent which left its own ARGV arguments in the environment.
  139.  
  140. Because the strings in the environment following an ARGV variable are
  141. not environment variables, a child should truncate its own environment
  142. at the ARGV variable by changing the 'A' to a null.
  143.  
  144. Implementation: Parental Responsibilities
  145.  
  146. To pass arguments in the environment, a parent must create an
  147. environment string for the child.  This can be achieved by first
  148. allocating as much space as is used in the parent's own environment,
  149. plus enough room for the ARGV variable and the arguments to the child,
  150. and then copying the parent's environment to the newly allocated area.
  151. Next, the ARGV variable must be appended, since it must be the last
  152. variable in the chi